﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace AnycleLiu.Common.Security
{
    /// <summary>
    /// 散列算法帮助类
    /// </summary>
    public class HashHelper
    {
        /// <summary>
        /// 散列运算
        /// </summary>
        /// <param name="data"></param>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static string ComputHash(byte[] data,string algorithmName="MD5")
        {
            HashAlgorithm hashAlgorithm=HashAlgorithm.Create(algorithmName);
            byte[] bytes=hashAlgorithm.ComputeHash(data);

            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// 密钥散列运算
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static string ComputHash(byte[] data, byte[] key, string algorithmName = "HMACMD5")
        {
            KeyedHashAlgorithm keyedHashAlgorithm = KeyedHashAlgorithm.Create(algorithmName);
            keyedHashAlgorithm.Key=key;
            byte[] bytes = keyedHashAlgorithm.ComputeHash(data);

            return Convert.ToBase64String(bytes);
        }
    }
}
